<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 组件-订单列表 开发者: 麦沃德科技-半夏 
+---------------------------------------------------------------------- -->

<template>
	<view class="component-order-list" :style="{'--theme-color': themeColor}">
		<view class="list-order" v-for="item in showData" :key="item.id" @click="toDetails(item.id)">
			<order-item :key="item.id" :show-data="item" :loading="loading">
				<template v-slot:header>
					<view class="order-header">
						<view class="header-label" :style="{'--label-color': themeColor}" v-if="item.state == 'pending'">
							<view class="background"></view>
							<text class="text">待接单</text>
						</view>
						<view class="header-label" :style="{'--label-color': '#00A743'}" v-else-if="item.state == 'accepted' || item.state == 'in_progress'">
							<view class="background"></view>
							<text class="text">行程中</text>
						</view>
						<view class="header-label" :style="{'--label-color': '#FFC330'}" v-else-if="item.state == 'awaiting_payment'">
							<view class="background"></view>
							<text class="text">待补差</text>
						</view>
						<view class="header-label" :style="{'--label-color': '#8D929C'}" v-else-if="item.state == 'completed'">
							<view class="background"></view>
							<text class="text">已完成</text>
						</view>
						<view class="header-label" :style="{'--label-color': '#8D929C'}" v-else-if="item.state == 'cancelled'">
							<view class="background"></view>
							<text class="text">已取消</text>
						</view>
						<view class="header-label" :style="{'--label-color': '#FFC330'}" v-else-if="item.state == 'refunded'">
							<view class="background"></view>
							<text class="text">退款中</text>
						</view>
					</view>
				</template>
				<template v-slot:footer>
					<view class="order-footer flex">
						<view class="footer-btn" v-if="item.state == 'pending' || item.state == 'accepted'" @click.stop="handleCancel(item.id)">取消订单</view>
						<view class="footer-btn" v-if="item.state == 'completed' && item.evaluation_state == 2" @click.stop="handleEvaluate(item.id, item.evaluation_state)">评价订单</view>
					</view>
				</template>
			</order-item>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import orderItem from "@/pages/passenger/component/order/item.vue"
	export default {
		name: "componentOrderList",
		props: ["showData", "loading"],
		components: {
			orderItem,
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		methods: {
			// 跳转详情
			toDetails(id) {
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/order/details?id=" + id,
					isToken: true,
				})
			},
			// 取消订单
			handleCancel(id) {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				this.$util.request("passenger.order.cancelConfig", { id }).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						const orderInfo = res.data
						if (orderInfo.state == 'accepted') {
							const endTime = (orderInfo.user_cancel_config?.user_free_cancel_end_time || 0) * 1000
							const nowTime = new Date().getTime()
							const timeDiff = endTime - nowTime;
							const orderData = {
								id: id,
								state: orderInfo.state,
								driver_arrive_status: orderInfo.driver_arrive_status,
								user_cancel_config: orderInfo.user_cancel_config || {},
								isTimeEnd: timeDiff > 0 ? 2 : 1
							}
							if (orderInfo.driver_arrive_status == 'not_arrived' && timeDiff > 0) {
								this.$emit("onCancel", orderData)
							} else if (parseInt(orderInfo.user_cancel_config?.user_every_day_cancel_count || 0) > 0) {
								uni.showModal({
									content: ` 今天您还有${orderInfo.user_cancel_config.user_every_day_cancel_count}次取消机会， \n 是否取消？ `,
									cancelText: "我再想想",
									cancelColor: "#8D929C",
									confirmText: "确认取消",
									confirmColor: this.themeColor,
									success: (res) => {
										if (res.confirm) {
											this.$emit("onCancel", orderData)
										}
									}
								})
							} else {
								uni.showModal({
									content: ` 取消本次订单，将扣除${orderInfo.user_cancel_config?.user_cancel_order_deduct_fees || 0}元给师傅补偿 `,
									cancelText: "我再想想",
									cancelColor: "#8D929C",
									confirmText: "确认取消",
									confirmColor: this.themeColor,
									success: (res) => {
										if (res.confirm) {
											this.$emit("onCancel", orderData)
										}
									}
								})
							}
						} else {
							const orderData = {
								id: id,
								state: orderInfo.state,
							}
							this.$emit("onCancel", orderData)
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('获取订单详情 ', error)
				})
			},
			// 评价订单
			handleEvaluate(id, state) {
				this.$emit("onEvaluate", id, state)
			},
		}
	}
</script>

<style lang="scss">
	.component-order-list {
		.list-order {
			margin-top: 32rpx;
			border-radius: 32rpx;
			background: #FFF;
			padding: 32rpx;

			&:first-child {
				margin-top: 0;
			}

			.order-header {
				margin-left: 16rpx;

				.header-label {
					position: relative;
					z-index: 1;
					padding: 0 4rpx;
					border-radius: 8rpx;
					overflow: hidden;
					height: 40rpx;
					line-height: 40rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					.background {
						background: var(--label-color);
						opacity: .1;
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						z-index: -1;
					}

					.text {
						color: var(--label-color);
						text-align: center;
						font-size: 24rpx;
						font-weight: 600;
					}
				}
			}

			.order-footer {
				.footer-btn {
					color: var(--theme-color);
					font-size: 24rpx;
					font-weight: 600;
					line-height: 34rpx;
					border-radius: 24rpx;
					border: 1px solid var(--theme-color);
					height: 48rpx;
					padding: 0 16rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-left: 16rpx;
				}
			}
		}
	}
</style>